package com.yenlien.traceyun.plant.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.plant.dao.AppVersionDao;
import com.yenlien.traceyun.plant.entity.AppVersion;
import com.yenlien.traceyun.plant.entity.Plant;
import com.yenlien.traceyun.plant.service.PlantService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 种植
 *
 * @author maybe
 * @date 2020年7月27日
 */
@RestController
@RequestMapping("/plant")
public class PlantController extends BaseController {
    @Autowired
    private PlantService plantService;

    @Autowired
    private AppVersionDao appVersionDao;

    @PostMapping(value = "/saveVersion")
    public Object save(@RequestBody AppVersion appVersion) {
        appVersion.setCreateTime(LocalDateTime.now());
        return success(appVersionDao.save(appVersion));
    }

    @GetMapping(value = "/getLast")
    public Object getLast() {
        AppVersion last = appVersionDao.getLast();
        last.setTimestamp(new Date().getTime());
        return last;
    }


    // 获取当前企业种植的地块名称和基源
    @GetMapping(value = "/getSoilJY")
    public Object getSoilJY(String companyId) {
        return success(plantService.getSoilJY(StringUtils.isEmpty(companyId) ? UserAuthUtil.getLoginUser().getCompanyId(): companyId));
    }



    // 根据地块获取地块下品种，气象数据信息
    @GetMapping(value = "/getDataBySoilId")
    public Object getDataBySoilId(String soilId) {
        return success(plantService.getDataBySoilId(soilId));
    }


    @GetMapping(value = "/getDevAlarm")
    public Object getDevAlarm(String compId) {
        return success(plantService.getDevAlarm(StringUtils.isEmpty(compId) ? UserAuthUtil.getLoginUser().getCompanyId() : compId));
    }








    //分页查询列表
//	@DataAuth
    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody JSONObject params) {
        Plant plant = new Plant();
        plant.setPlantName(params.getString("plantName"));
        plant.setSoilName(params.getString("soilName"));
        plant.setJyName(params.getString("jyName"));
        plant.setPage(params.getIntValue("page"));
        plant.setPageSize(params.getIntValue("pageSize"));
        if (!StringUtils.isEmpty(params.getString("companyId"))) {
            plant.setCompanyId(params.getString("companyId"));
        } else {
            plant.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        }
        return buildPage(plantService.findByList(plant));
    }


    @RequestMapping(value = "/findByListApp", method = RequestMethod.POST)
    public Object findByListApp(@RequestBody JSONObject params) {
        Plant plant = new Plant();
        plant.setPlantName(params.getString("plantName"));
        plant.setSoilName(params.getString("soilName"));
        plant.setJyName(params.getString("jyName"));
        plant.setReapStatus(params.getIntValue("reapStatus"));
        plant.setPage(params.getIntValue("page"));
        plant.setPageSize(params.getIntValue("pageSize"));
        if (!StringUtils.isEmpty(params.getString("companyId"))) {
            plant.setCompanyId(params.getString("companyId"));
        } else {
            plant.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        }
        return buildPage(plantService.findByListApp(plant));
    }


    //根据ID查询详情
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable("id") String id) {
        return success(plantService.findById(id));
    }

    /**
     * /根据ID查询详情
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/view/{id}")
    public Object view(@PathVariable("id") String id) {
        return plantService.view(id, UserAuthUtil.getLoginUser());
    }

    //新增
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(Plant plant, HttpServletRequest request) throws Exception {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        plantService.save(plant, files);
        return success("success");
    }

    @RequestMapping(value = "/save2", method = RequestMethod.POST)
    public Object save2(@RequestBody Plant plant) throws Exception {
        plantService.save2(plant);
        return success("success");
    }

    //修改
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(@RequestBody Plant plant) {
        plantService.update(plant);
        return success("success");
    }

    //根据基地ID查询种植列表
    @RequestMapping(value = "/findByPlantBaseId/{plantBaseId}", method = RequestMethod.GET)
    public Object findByPlantBaseId(@PathVariable("plantBaseId") String id) {
        return success(plantService.findByPlantBaseId(id));
    }

    //根据基地ID查询种植列表
    @RequestMapping(value = "/findBySoilId/{areaId}", method = RequestMethod.GET)
    public Object findBySoilId(@PathVariable("areaId") String id) {
        List<Plant> plants = plantService.findByAreaId(id);
        if (plants != null && plants.size() > 0) {
            String jyId = plants.get(0).getJyId();
        }
        return success(plantService.findByAreaId(id));
    }


    /**
     * 剩余面积
     *
     * @return
     */
    @GetMapping(value = "/leftarea/{soilId}")
    public Object usedarea(@PathVariable("soildId") String soilId) {
        return success(plantService.leftarea(soilId));
    }


    /**
     * 查询所有生产监管种植面积
     *
     * @return
     */
    @PostMapping("/areas")
    public Object areas() {
        return success(plantService.areas(UserAuthUtil.getLoginUser().getCompanyId()));
    }

    /**
     * 查询公司生产监管种植面积
     *
     * @return
     */
    @PostMapping("/area")
    public Object area(@RequestBody JSONObject param) {
        String companyId = param.getString("companyId");
        return success(plantService.area(companyId));
    }

    /**
     * 生产管理生产概况饼形图
     *
     * @return
     */
    @PostMapping("/yield")
    public Object yield() {
        return success(plantService.yield(UserAuthUtil.getLoginUser().getCompanyId()));
    }

    /**
     * 地块数据回显
     *
     * @return
     */
    @PostMapping("/soil")
    public Object soil(@RequestBody JSONObject param) {
        return success(plantService.soil(param.getString("soil")));
    }


    @PostMapping("/findByBaseSoilId")
    public Object findByBaseSolieId(@RequestBody JSONObject param) {
        String str = param.get("baseSoilId") == null ? "" : param.get("baseSoilId").toString();
        return success(plantService.findByBaseSolieId(str));
    }

}
